import Vue from 'vue'
import Router from 'vue-router'
import store from '@/store'
import constant from '@/public/constant'

Vue.use(Router)


const	routes = [
		{
			path: '/',
			redirect: constant.userAgent==='web'?'/login':'/home'
        },
        {
            path:'/login',
            name:'login',
            component: () => import('@/views/login')
        },
		{
			path: '/home',
			name: 'home',
			component: () =>
			import('@/views/home'),
			redirect: '/home/mine',
			children: [
				{
					path: 'mine',
					name: 'mine',
					component: () =>
						import('@/views/mine')
				},
				{
					path: 'system',
					name: 'system',
					component: () =>
						import('@/views/system')
				},
				{
					path: 'masterData',
					name: 'masterData',
					component: () =>
						import('@/views/masterData')
				},
				{
					path:'accountManagement',
					name:"accountManagement",
					component:()=>
						import('@/views/accountManagement')
				},
				{
					path:'purchase',
					name:"purchase",
					component:()=>
						import('@/views/purchase')
				},
				{
					path:'dispatch',
					name:'dispatch',
					component:()=>
						import('@/views/sendLeafletSystem')
				},
				{
					path:'sales',
					name:'sales',
					component:()=>
						import('@/views/sales')
				},
				{
					path:'myLogistics',
					name:'myLogistics',
					component:()=>
						import('@/views/myLogistics')
				},
				{
					path:'purchaseReport',
					name:'purchaseReport',
					component:()=>
						import('@/views/purchaseReport')
				},
				{
					path:'comprehensive',
					name:'comprehensive',
					component:()=>
						import('@/views/comprehensive')
                },
                {
					path:'setting',
					name:'setting',
					component:()=>
						import('@/views/setting')
				},
				{
					path:'authServiceManager',
					name:'authServiceManager',
					component:()=>
						import('lesso-component-log/src/index')
                },
                {
					path:'financingManager',
					name:'financingManager',
					component:()=>
						import('@/views/financeManager')
				},
				{
					path:'appointmentUnloading',
					name:'appointmentUnloading',
					component:()=>
						import('@/views/appointmentUnloading')
                },
                {
					path:'about',
					name:'about',
					component:()=>
						import('@/views/about')
				},
				{
					path:'matterApply',
					name:'matterApply',
					component:()=>
						import('@/views/matterApply')
				}
			]
		},
		{
			path: '*',
			redirect:  constant.userAgent==='web'?'/login':'/home'
		}
	]
const router = new Router({
    routes
})
if(constant.userAgent=="web"){
    router.beforeEach ((to, from, next)=>{  
        const token = store.state.app.userData.token
        // 未登录跳到登录页，已登录刷新保存当前路由
        if (!token&&to.name !== 'login') {
            next({
                name: 'login'
            })
        } else if(!token&&to.name == 'login'){
            next()
        } else if(token&&to.name == 'login') {
            next({
                name:store.state.app.currentRoute
            })
        } else {
            next()
        }
    })
}
export default router
